# -*- coding: utf-8 -*-

import pymysql
import matplotlib.pyplot as plt
import SparkAPI

# ##获取一个数据库连接，注意如果是UTF-8类型的，需要制定数据库
# db = pymysql.connect(host="localhost", user='root', passwd="123456", port=3306, db="student", charset='utf8')
# cursor = db.cursor()  # 获取一个游标
# sql = "select name,chineseScore from student_info"
# cursor.execute(sql)
# result = cursor.fetchall()  # result为元组
# print(result)

def paintChart(sqlResult):
    # 将元组数据存进列表中
    name = []
    chineseScore = []

    data = sqlResult

    names = [d["name"] for d in data]
    scores = [d["chineseScore"] for d in data]

    result = list(zip(names, scores))
    print(result)
    # for x in result:
    #     name.append(x[0])
    #     chineseScore.append(x[1])
    #
    # # 直方图
    # plt.bar(range(len(chineseScore)), chineseScore, color='steelblue', tick_label=name)
    # plt.xlabel("name")
    # plt.ylabel("chineseScore")
    # plt.title("student")
    # for x, y in enumerate(chineseScore):
    #     plt.text(x - 0.4, y + 0.4, '%s' % y)
    # plt.show()

if __name__ == '__main__':
    paintChart([{"name": "jack", "chineseScore": 86}, {"name": "peter", "chineseScore": 60}, {"name": "dam", "chineseScore": 90}, {"name": "alie", "chineseScore": 77}, {"name": "sdf", "chineseScore": 88}])

